package edu.xlh.travel.service.impl;

import edu.xlh.travel.common.Result;
import edu.xlh.travel.dao.TotalScoreDao;
import edu.xlh.travel.pojo.Scenic;
import edu.xlh.travel.pojo.SysLog;
import edu.xlh.travel.pojo.TotalScore;
import edu.xlh.travel.pojo.User;
import edu.xlh.travel.service.TotalScoreService;
import edu.xlh.travel.util.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class  TotalScoreServiceImpl implements TotalScoreService {
    @Autowired
    private TotalScoreDao totalScoreDao;
    @Override
    public TotalScore findByUserId(String userId) {
        return totalScoreDao.findByUserId(userId);
    }

    @Override
    public void updateTotalScoreByUserId(String id , Integer totalScore) {
        totalScoreDao.updateTotalScoreByUserId(id , totalScore);
    }

    @Override
    public Result findAllTotalScore(int page, int limit, HttpSession session) {
        User user = (User)session.getAttribute("admin");
        if (user == null) {
            return new Result(201);
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "modifyTime");
        Pageable pageable= PageRequest.of(page-1,limit ,sort);
        Page<TotalScore> totalScoresPage = totalScoreDao.findAll(pageable);
        if (totalScoresPage!=null) {
            return new Result(0 , "总积分分页数据获取成功" , new PageResult<TotalScore>(totalScoresPage.getTotalElements(), totalScoresPage.getContent()));
        }
        return new Result(201, "总积分分页数据获取失败");
    }

    @Override
    public Result getTotalScoreByUserId(HttpSession session) {
        User user = (User)session.getAttribute("user");
        if (user == null) {
            return new Result(201 , "请先登录");
        }
        TotalScore totalScore = totalScoreDao.findByUserId(user.getId());
        return new Result(200 , (Object)totalScore.getTotalScore());
    }

    @Override
    public Result QueryTotalScoreByCondition(Integer page, Integer limit, TotalScore totalScore, HttpSession session) {
        User userFromSession = (User)session.getAttribute("admin");
        if (userFromSession == null) {
            return new Result(201,"会话过期，请重新登陆");
        }
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> totalScoreList = new ArrayList<>();
                if (StringUtils.isNotEmpty(totalScore.getUserName())) {
                    totalScoreList.add(criteriaBuilder.like(root.get("name") , "%" + totalScore.getUserName()));
                }
                if (totalScore.getTotalScore() != null) {
                    totalScoreList.add(criteriaBuilder.between(root.get("totalScore") ,  Integer.parseInt(totalScore.getTotalScore()) - 5 , Integer.parseInt(totalScore.getTotalScore()) + 5));
                }
                return criteriaBuilder.and(totalScoreList.toArray(new Predicate[totalScoreList.size()]));
            }
        };
        Pageable pageable= PageRequest.of(page-1,limit);
        Page<TotalScore> totalScoreListPage  = totalScoreDao.findAll(specification , pageable);
        if (totalScoreListPage!=null) {
            return new Result(0 , "积分分页数据获取成功" , new PageResult<TotalScore>(totalScoreListPage.getTotalElements(), totalScoreListPage.getContent()));
        }
        return new Result(201 , "积分景点列表失败");
    }

    @Override
    public void updateTotalScoreByUserIdForPay(String userId, String totalScore) {
        totalScoreDao.updateTotalScoreByUserIdForPay(userId , totalScore);
    }

    @Override
    public void save(TotalScore totalScore) {
        totalScoreDao.save(totalScore);
    }
}
